#include "options.h"

#include <cstdlib>
#include <getopt.h>
#include <iostream>
#include <sstream>

struct option long_options[] =
{
  {"help", no_argument, 0, 'h'},
// {"trace-length",  required_argument, 0, 'N'},
  {"wavelength-start",  required_argument, 0, 'a'},
  {"wavelength-stop",  required_argument, 0, 'b'},
  {"resolution-bandwidth",  required_argument, 0, 'w'},
  {"reference-level",  required_argument, 0, 'r'},
  {"sensitivity",  required_argument, 0, 's'},
  {"batch",  no_argument, 0, 'B'},
  {"line-begin",  required_argument, 0, 'g'},
  {"line-count",  required_argument, 0, 'l'},
  {"line-height", required_argument, 0, 'd'},
  {"title", required_argument, 0, 't'},
  {0, 0, 0, 0}
};

char* help_msg[] =
{
  "Display help",
//  "Number of data points",
  "Lower wavelength in NM",
  "Upper wavelength in NM",
  "Resolution bandwidth in NM",
  "Reference level in dBM",
  "Amplitude sensitivity in dBM",
  "Enable batch line scan",
  "Set beginning position for batch scan in MM",
  "Set number of lines for batch scan",
  "line height in MM",
  "description of the acquisition"
};

void printHelp()
{

  std::cout << "osa_record_traces: Capture and save measurements from Agilent OSA" << std::endl;
  for (int i=0; long_options[i].val !=0; i++)
    {
      std::cout << '-' << (char)long_options[i].val << ' '
                << "--"  << long_options[i].name
                << "\t\t" << help_msg[i] << std::endl;
    }
}

bool parseOptions(int argc, char *argv[],
                  //  unsigned *trace_length,
                  unsigned *wavelength_start, unsigned *wavelength_stop,
                  float *bw_res, float *ref_level,
                  float *sensitivity,
                  bool *batch_scan,
                  double *beginning_displacement,
                  unsigned *number_of_scans,
                  double *line_height,
				  std::string *title
                 )
{
  /* getopt_long stores the option index here. */
  int option_index = 0;

  while (1)
    {
      int c = getopt_long (argc, argv, "ha:b:w:r:s:mBg:l:d:t::",
                           long_options, &option_index);
      char comma = ',';
      /* Detect the end of the options. */
      if (c == -1)
        break;
      switch (c)
        {
		case '?':
		// Invalid option
		  break;
        case 'h':
          printHelp();
          return 0;
          break;
//       case 'N':
//         *trace_length = std::atol(optarg);
//          break;
        case 'a':
          *wavelength_start = std::atol(optarg);
          break;
        case 'b':
          *wavelength_stop = std::atol(optarg);
          break;
        case 'w':
          *bw_res = std::atof(optarg);
          break;
        case 'r':
          *ref_level = std::atof(optarg);
          break;
        case 's':
          *sensitivity = std::atof(optarg);
          break;
        case 'B':
          *batch_scan = 1;
          break;
        case 'g':
          *beginning_displacement = std::atof(optarg);
          break;
        case 'l':
          *number_of_scans = std::atol(optarg);
          break;
        case 'd':
          *line_height = std::atof(optarg);
          break;
		case 't':
          *title = std::string(optarg);
		  break;
        default:
          throw "error parsing options";
        }
    }
  return 1; //success
}



